import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)
//引入elementui
import ELementUI, {Message} from 'element-ui'
Vue.use(ELementUI)

import Login from '@/components/manager/Login.vue'
import Home from '@/components/Home.vue'
import Welcome from '@/components/WelCome.vue'
import Permission from "@/components/system/Permission.vue";
import Role from "@/components/system/Role.vue";
import Manager from "@/components/system/Manager.vue";

import Tree from "@/components/test/TestTree.vue";
import TestTree from "@/components/test/TestTree.vue";
import Book from '@/components/books/Book.vue'
import BookType from '@/components/books/BookType.vue'
import User from '@/components/user/User.vue'

const routes = [
  {
    path:'/',
    name:'Login',
    component:Login
  },
  {
    path:'/Login',
    name:'Login',
    component:Login
  },
  {
    path:'/Home',
    name:'Home',
    component:Home ,
    children:[
      {
        path:'/',
        name:'Welcome',
        component:Welcome
      },
      {
        path:'/Permission',
        name:'Permission',
        component:Permission
      },
      {
        path:'/Role',
        name:'Role',
        component:Role
      },
      {
        path:'/TestTree',
        name:'TestTree',
        component:TestTree
      },
      {
        path:'/Manager',
        name:'Manager',
        component:Manager
      },
      {
        path:'/Book',
        name:'Book',
        component:Book
      },
      {
        path:'/BookType',
        name:'BookType',
        component:BookType
      },
      {
        path:'/User',
        name:'User',
        component:User
      }
    ]
  }
]
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由守卫
router.beforeEach((to,from,next)=>{
  let path = to.path ;
  if (path == '/' || path == '/Login'){
    next();
  }else {
    let token = sessionStorage.getItem('token');
    if (token) {
      next();
    } else {
      Message({
        type:'error',
        message:'请先登录',
        duration:1000
      })
      next('/');
    }
  }
})




export default router
